﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace MyFloridaHouse.libSvOpenDistricts.SummaryDataGrid
{
    public static class SummaryDataGridIOCSV
    {
        public static MemoryStream ToCSV(this SummaryDataGridManager summaryDataGrid, string fileName)
        {
            MemoryStream CSVmemoryStream = new MemoryStream();
            StreamWriter CSVstreamWriter = new StreamWriter(CSVmemoryStream);

            var columnDescriptionHeadings = from descr in summaryDataGrid.summaryDataGridColumnDescriptionList
                                            select descr.header;
            //write the column headings
            List<string> columnHeaderNames = new List<string>();
            columnHeaderNames.AddRange(columnDescriptionHeadings);
            CSVstreamWriter.WriteLine(string.Join(",", columnHeaderNames));

            // write the data
            foreach (var dataGridRow in summaryDataGrid.districtSummaryDataGridList)
            {
                string newLine = string.Join(",",dataGridRow.valuesList);
                CSVstreamWriter.WriteLine(newLine);
            }
            CSVstreamWriter.Flush();
            return  CSVmemoryStream;
        }
    }
}
